package com.neusoft.shopping.manager.dao;

import com.neusoft.shopping.manager.entity.StockEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StockDAOImpl {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    RowMapper<StockEntity> rowMapper=new BeanPropertyRowMapper<>(StockEntity.class);

    public List<StockEntity> qureyAll(){
       String  sql="select *from stockinfo";
       return jdbcTemplate.query(sql,rowMapper);
    }

    public StockEntity qureyBystockno(Integer stockno){
        String  sql="select *from stockinfo where stockno=?";
        return jdbcTemplate.queryForObject(sql,rowMapper,stockno);
    }

    public int add_stock(StockEntity stock){
        String sql="insert into stockinfo values (null,?,?,?,?)";
        return jdbcTemplate.update(sql,stock.getGoodsno(),stock.getGoodsquantity(),stock.getWarningvalue(),stock.getState());
    }

    public int update_stockByGoodsno(StockEntity stock){
        String sql="update stockinfo set goodsquantity=?,warningvalue=?,state=? where stockno=?";
        return jdbcTemplate.update(sql,stock.getGoodsquantity(),stock.getWarningvalue(),stock.getState(),stock.getStockno());
    }
    public int del_stock(Integer stockno){
        String sql="delete from stockinfo where stockno=?";
        return jdbcTemplate.update(sql,stockno);
    }
}
